package com.py.inbound.service.impl;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.py.inbound.domain.OdPurchaseOrder;
import com.py.inbound.domain.PyAttachment;
import com.py.inbound.domain.PyInboundDetalis;
import com.py.inbound.domain.WhInventory;
import com.py.inbound.mapper.PyAttachmentMapper;
import com.py.inbound.mapper.PyInboundDetalisMapper;
import com.py.inbound.mapper.PyInboundMapper;
import com.py.inbound.service.IPyInboundDetalisService;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-02
 */
@Service
public class PyInboundDetalisServiceImpl implements IPyInboundDetalisService {
    @Resource
    private PyInboundDetalisMapper pyInboundDetalisMapper;
    @Resource
    private PyInboundMapper pyInboundMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param rkDetailsId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public PyInboundDetalis selectPyInboundDetalisByRkDetailsId(Long rkDetailsId) {
        return pyInboundDetalisMapper.selectPyInboundDetalisByRkDetailsId(rkDetailsId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param pyInboundDetalis 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<PyInboundDetalis> selectPyInboundDetalisList(PyInboundDetalis pyInboundDetalis) {
        return pyInboundDetalisMapper.selectPyInboundDetalisList(pyInboundDetalis);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param pyInboundDetalis 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertPyInboundDetalis(PyInboundDetalis pyInboundDetalis) {
        return pyInboundDetalisMapper.insertPyInboundDetalis(pyInboundDetalis);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param pyInboundDetalis 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePyInboundDetalis(List<PyInboundDetalis> pyInboundDetalis) {
//        System.out.println("pyInboundDetalis:" + pyInboundDetalis);
        for (int i = 0; i < pyInboundDetalis.size(); i++) {
            WhInventory whInventory1 = new WhInventory();
//            System.out.println("getRkDetailsId:" + pyInboundDetalis.get(i).getRkDetailsId());
            // 获取新的入库数量
            PyInboundDetalis detalisList = pyInboundDetalisMapper.SelectquantityThisTime(pyInboundDetalis.get(i).getRkDetailsId());
//            System.out.println("detalisList:" + detalisList);
            int previousQuantity = detalisList.getQuantityThisTime(); // 之前的数量
            whInventory1.setPendingIn(previousQuantity); // 设置待减数量
            whInventory1.setProductCode(detalisList.getProductCode());
            whInventory1.setLocationCode(detalisList.getLocationCode());

            // 执行减库存操作
            int i2 = pyInboundDetalisMapper.updateJianWhInventoryPendingIn(whInventory1);
//            System.out.println("修改先减" + i2);
        }
        PyInboundDetalis firstDetail = pyInboundDetalis.get(0);
        int i1 = pyInboundDetalisMapper.deletePyInboundDetalisByRkDetailsIdINTER(firstDetail.getInventoryNumber());

        // 计算新的入库金额
        for (PyInboundDetalis p1 : pyInboundDetalis) {
            BigDecimal inboundPrice = p1.getInboundPrice(); // 获取入库价格
            Integer quantityThisTime = p1.getQuantityThisTime(); // 获取本次入库数量
            BigDecimal quantityBigDecimal = BigDecimal.valueOf(quantityThisTime); // 转换为 BigDecimal
            BigDecimal totalPrice = inboundPrice.multiply(quantityBigDecimal); // 计算总价格
            p1.setInboundAmount(totalPrice);
        }


        // 先减去之前的数量

        // 插入新的入库明细
        int add = pyInboundDetalisMapper.insertPyInboundDetalisList(pyInboundDetalis);
        if (add > 0) {
            for (PyInboundDetalis p2 : pyInboundDetalis) {

                WhInventory whInventory1 = new WhInventory();
                // 然后加上新的入库数量
                whInventory1.setPendingIn(p2.getQuantityThisTime()); // 设置为新的入库数量
                whInventory1.setProductCode(p2.getProductCode());
                whInventory1.setLocationCode(p2.getLocationCode());

//                System.out.println("whInventory:" + whInventory1);
                int i = pyInboundDetalisMapper.updateupdateWhInventoryPendingIn(whInventory1);
//                System.out.println("修改库存表未入库数量:" + i);
            }
        }

        return add;

    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param rkDetailsIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePyInboundDetalisByRkDetailsIds(Long[] rkDetailsIds) {
        return pyInboundDetalisMapper.deletePyInboundDetalisByRkDetailsIds(rkDetailsIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param rkDetailsId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePyInboundDetalisByRkDetailsId(Long rkDetailsId) {
        return pyInboundDetalisMapper.deletePyInboundDetalisByRkDetailsId(rkDetailsId);
    }


    //入库明细添加
    @Override
    @Transactional // 添加事务管理
    public int insertPyInboundDetalisList(List<PyInboundDetalis> pyInboundDetalis1) {
        Integer goodNum = 0;
        for (PyInboundDetalis p1 : pyInboundDetalis1) {
//            System.out.println("嘎嘎嘎:" + p1);
            // 假设 p1 是你的 PyInboundDetails 对象
            BigDecimal inboundPrice = null;
            if (p1.getInboundPrice() == null) {
                inboundPrice = p1.getSalePrice(); // 获取退货单入库价格
                p1.setInboundPrice(p1.getSalePrice());
            } else {
                inboundPrice = p1.getInboundPrice(); // 获取入库价格
            }
            if (p1.getQuantityReceived() == null) {
                p1.setQuantityReceived(p1.getAlreadyNum());
            }
            Integer quantityThisTime = p1.getQuantityThisTime(); // 获取本次入库数量
            // 将 Integer 转换为 BigDecimal
            BigDecimal quantityBigDecimal = BigDecimal.valueOf(quantityThisTime); // 转换为 BigDecimal
            // 进行乘法运算
            BigDecimal totalPrice = inboundPrice.multiply(quantityBigDecimal); // 计算总价格

            p1.setInboundAmount(totalPrice);
//            System.out.println("未入库数量:" + p1.getQuantityPending());
//            p1.setQuantityPending(p1.getQuantityThisTime());
        }
        int add = pyInboundDetalisMapper.insertPyInboundDetalisList(pyInboundDetalis1);
        if (add > 0) {
            for (PyInboundDetalis p2 : pyInboundDetalis1) {
                WhInventory whInventory = new WhInventory();
                whInventory.setPendingIn(p2.getQuantityThisTime());
                whInventory.setProductCode(p2.getProductCode());
                whInventory.setLocationCode(p2.getLocationCode());
                int i = pyInboundDetalisMapper.updateupdateWhInventoryPendingIn(whInventory);
                System.out.println("修改库存表未入库数量:" + i);
                if (i == 0) {
                    System.out.println("进");
                    whInventory.setCurrentInventory(0);
//                    whInventory.setProductCode(p2.getProductCode());
                    System.out.println("id:" + p2.getWarehouseId());
                    String s = pyInboundDetalisMapper.SelectwarehouseCode(p2.getLocationCode());
                    System.out.println("编号:" + s);
                    whInventory.setWarehouseCode(pyInboundDetalisMapper.SelectwarehouseCode(p2.getLocationCode()));
                    whInventory.setPendingOut(0);
//                    whInventory.setLocationCode(p2.getLocationCode());
                    BigDecimal inboundPrice = null;
                    if (p2.getInboundPrice() == null) {
                        inboundPrice = p2.getSalePrice(); // 获取退货单入库价格
                        p2.setInboundPrice(p2.getSalePrice());
                    } else {
                        inboundPrice = p2.getInboundPrice(); // 获取入库价格
                    }
                    if (p2.getQuantityReceived() == null) {
                        p2.setQuantityReceived(p2.getAlreadyNum());
                    }
                    Integer quantityThisTime = p2.getQuantityThisTime(); // 获取本次入库数量
                    // 将 Integer 转换为 BigDecimal
                    BigDecimal quantityBigDecimal = BigDecimal.valueOf(quantityThisTime); // 转换为 BigDecimal
                    // 进行乘法运算
                    BigDecimal totalPrice = inboundPrice.multiply(quantityBigDecimal); // 计算总价格
                    whInventory.setInventoryValue(totalPrice);
                    whInventory.setAvailableInventoryValue(totalPrice);
                    whInventory.setInventoryUpperLimit(100);
                    whInventory.setInventoryLowerLimit(10);
                    pyInboundDetalisMapper.insertWhInventory(whInventory);
                }
            }


        }
        return add;
    }

    @Override
    public int updateupdateWhInventoryPendingIn(WhInventory whInventory) {

        return 0;
    }

    @Override
    public List<PyInboundDetalis> selectAllPyInboundDetalisBycode(String code) {

        return pyInboundDetalisMapper.selectAllPyInboundDetalisBycode(code);
    }

    @Override
    public List<PyInboundDetalis> selectAllPyInboundDetalisBycodeXQ(String code) {
        List<PyInboundDetalis> pyInboundDetalis = pyInboundDetalisMapper.selectAllPyInboundDetalisBycodeXQ(code);
        for (PyInboundDetalis p : pyInboundDetalis
        ) {
            p.setRKhkc(p.getQuantityThisTime() + p.getInventoryValue());
        }
//        System.out.println("实打实大大:" + pyInboundDetalis);
        return pyInboundDetalis;
    }

    @Override
    public List<PyInboundDetalis> SelectAllupdatePyInboundDetalis(String code) {

        List<PyInboundDetalis> pyInboundDetalis = pyInboundDetalisMapper.SelectAllupdatePyInboundDetalis(code);
//        System.out.println("嘻嘻嘻:" + pyInboundDetalis);

        for (PyInboundDetalis p : pyInboundDetalis
        ) {
//            System.out.println("ccc:" + p);
            if (p.getQuantityPending() == 0) {

                p.setQuantityPending(p.getProductNum());
            }
            BigDecimal productNum = new BigDecimal(p.getProductNum().toString());
            BigDecimal inboundPrice = p.getInboundPrice();
            BigDecimal inboundAmount = productNum.multiply(inboundPrice);
            p.setInboundAmount(inboundAmount);
        }


        return pyInboundDetalis;
    }

    @Override
    public List<PyInboundDetalis> SelectAllupdatePyInboundDetalisTH(String code) {
        List<PyInboundDetalis> pyInboundDetalis = pyInboundDetalisMapper.SelectAllupdatePyInboundDetalisTH(code);
//        System.out.println("嘻嘻嘻11:" + pyInboundDetalis);
        return pyInboundDetalis;
    }

    @Override
    public WhInventory SelectWhInventoryInventoryUpperLimit(WhInventory whInventory) {
        return pyInboundDetalisMapper.SelectWhInventoryInventoryUpperLimit(whInventory);
    }
}



